package com.octopus.optiworks.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;

import com.octopus.optiworks.model.Ituch;
import com.octopus.optiworks.model.Itunm;
import com.octopus.optiworks.model.Employee;
import com.octopus.optiworks.service.UserService;
import com.octopus.optiworks.service.UtilService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

	private DictionaryValidator dictionaryValidator = new DictionaryValidator();

	@Autowired
    private UtilService utilService;
	
	@Autowired
    private UserService userService;

    @RequestMapping("/ituch/manage.o")
    @RequiresPermissions("dictionary:manage")
    public void manageItuch(Model model) {
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
        model.addAttribute("ituchs", utilService.getAllItuchs());
    }

    @RequestMapping(value="/ituch/edit.o",method= RequestMethod.GET)
    @RequiresPermissions("dictionary:edit")
    public String showEditItuchForm(Model model, @RequestParam Long ituchId, @ModelAttribute Ituch ituch) {
    	Employee cu =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", cu);
    	model.addAttribute("action", "Edit");
    	Ituch it = utilService.getItuch(ituchId);
    	ituch.setId(ituchId);
    	ituch.setName(it.getName());
    	ituch.setDescription(it.getDescription());
        return "dictionary/ituch/edit";
    }

    @RequestMapping(value="/ituch/edit.o",method= RequestMethod.POST)
    @RequiresPermissions("dictionary:edit")
    public String editItuch(Model model, @RequestParam Long ituchId, @ModelAttribute Ituch ituch, BindingResult errors) {
    	dictionaryValidator.validate(ituch, errors );
        if( errors.hasErrors() ) {
        	model.addAttribute("action", "Edit");
            return "dictionary/ituch/edit";
        }
        ituch.setId(ituchId);
        try{
        	utilService.updateItuch(ituch);
        }catch (org.springframework.jdbc.UncategorizedSQLException e){
        	e.printStackTrace();
        	errors.rejectValue( "name", "error.name.exist", "The name has already existed." );
        	model.addAttribute("action", "Edit");
            return "dictionary/ituch/edit";
        }
        return "redirect:/dictionary/ituch/manage.o";
    }

    @RequestMapping("/ituch/delete.o")
    @RequiresPermissions("dictionary:delete")
    public String deleteItuch(@RequestParam Long ituchId) {
        utilService.deleteItuch( ituchId );
        return "redirect:/dictionary/ituch/manage.o";
    }
    
    @RequestMapping(value="/ituch/create.o",method= RequestMethod.GET)
    public String showCreateItuchForm(Model model, @ModelAttribute Ituch ituch) {
    	Employee cu =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", cu);
    	model.addAttribute("action", "Create");
        return "dictionary/ituch/edit";
    }

    @RequestMapping(value="/ituch/create.o",method= RequestMethod.POST)
    public String createItuch(Model model, @ModelAttribute Ituch ituch, BindingResult errors) {
    	dictionaryValidator.validate(ituch, errors );
        if( errors.hasErrors() ) {
        	model.addAttribute("action", "Create");
            return "dictionary/ituch/edit";
        }
        try{
        	utilService.createItuch(ituch);
        }catch (org.springframework.dao.DataIntegrityViolationException e){
        	e.printStackTrace();
        	errors.rejectValue( "name", "error.name.exist", "The name has already existed." );
        	model.addAttribute("action", "Create");
            return "dictionary/ituch/edit";
        }
        return "redirect:/dictionary/ituch/manage.o";
    }

    @RequestMapping("/itunm/manage.o")
    @RequiresPermissions("dictionary:manage")
    public void manageItunm(Model model) {
    	Employee user =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", user);
        model.addAttribute("itunms", utilService.getAllItunms());
    }

    @RequestMapping(value="/itunm/edit.o",method= RequestMethod.GET)
    @RequiresPermissions("dictionary:edit")
    public String showEditItunmForm(Model model, @RequestParam Long itunmId, @ModelAttribute Itunm itunm) {
    	Employee cu =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", cu);
    	model.addAttribute("action", "Edit");
    	Itunm it = utilService.getItunm(itunmId);
    	itunm.setId(itunmId);
    	itunm.setName(it.getName());
    	itunm.setDescription(it.getDescription());
        return "dictionary/itunm/edit";
    }

    @RequestMapping(value="/itunm/edit.o",method= RequestMethod.POST)
    @RequiresPermissions("dictionary:edit")
    public String editItunm(Model model, @RequestParam Long itunmId, @ModelAttribute Itunm itunm, BindingResult errors) {
    	dictionaryValidator.validate(itunm, errors );
        if( errors.hasErrors() ) {
        	model.addAttribute("action", "Edit");
            return "dictionary/itunm/edit";
        }
        itunm.setId(itunmId);
        try{
        	utilService.updateItunm(itunm);
        }catch (org.springframework.jdbc.UncategorizedSQLException e){
        	e.printStackTrace();
        	errors.rejectValue( "name", "error.name.exist", "The name has already existed." );
        	model.addAttribute("action", "Edit");
            return "dictionary/itunm/edit";
        }
        return "redirect:/dictionary/itunm/manage.o";
    }

    @RequestMapping("/itunm/delete.o")
    @RequiresPermissions("dictionary:delete")
    public String deleteItunm(@RequestParam Long itunmId) {
        utilService.deleteItunm( itunmId );
        return "redirect:/dictionary/itunm/manage.o";
    }
    
    @RequestMapping(value="/itunm/create.o",method= RequestMethod.GET)
    public String showCreateItunmForm(Model model, @ModelAttribute Itunm itunm) {
    	Employee cu =  this.userService.getCurrentUser();
    	model.addAttribute("currentUser", cu);
    	model.addAttribute("action", "Create");
        return "dictionary/itunm/edit";
    }

    @RequestMapping(value="/itunm/create.o",method= RequestMethod.POST)
    public String createItunm(Model model, @ModelAttribute Itunm itunm, BindingResult errors) {
    	dictionaryValidator.validate(itunm, errors );
        if( errors.hasErrors() ) {
        	model.addAttribute("action", "Create");
            return "dictionary/itunm/edit";
        }
        try{
        	utilService.createItunm(itunm);
        }catch (org.springframework.dao.DataIntegrityViolationException e){
        	e.printStackTrace();
        	errors.rejectValue( "name", "error.name.exist", "The name has already existed." );
        	model.addAttribute("action", "Create");
            return "dictionary/itunm/edit";
        }
        return "redirect:/dictionary/itunm/manage.o";
    }

}
